package com.glory.dgcpcpda.entity.response;

import android.content.Context;

import androidx.fragment.app.FragmentManager;

import com.glory.dgcpcpda.base.MyApp;
import com.glory.dgcpcpda.entity.Exception;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.io.Serializable;
import java.util.ArrayList;

public class BaseResponseBean<T> implements Serializable {

    private ReturnBean<T> Response;

    private transient FreeUI_GeneralFragmentDialog tipDialog;


    public ReturnBean<T> getResponse() {
        return Response;
    }

    public void setResponse(ReturnBean<T> response) {
        this.Response = response;
    }

    //获取到对象后，必须要判断是否获取成功
     public boolean isSuccess(Context context, boolean isHaveBody, FragmentManager fragmentManager) {
         if (Response.getHeader().getRESULT().toUpperCase().equals("SUCCESS")) {
             if (isHaveBody) {
                 if (Response.getBody() == null || Response.getBody().equals("")) {
                     showFailDialog("暂无请求内容",fragmentManager);
                     return false;
                 } else {
                     if (Response.getBody() instanceof ArrayList) {
                         if ((((ArrayList) Response.getBody()).size()) > 0) {
                             return true;
                         } else {
                             showFailDialog("暂无请求数据",fragmentManager);
                             return false;
                         }
                     } else {
                         return true;
                     }
                 }
             } else {
                 return true;
             }
         } else {
             showFailDialog(getMessage(Response.getHeader()),fragmentManager);
             return false;
         }
     }


     private void showFailDialog(String message, FragmentManager fragmentManager)
    {
        tipDialog = FreeApi_DialogUtils.getConfirmDialog(MyApp.app, "确定", message, () -> {

        });
        tipDialog.show(fragmentManager,"DIALOG");
    }

    public static String getMessage(ResponseHeader resultCode){
        String message = "";
//        Exception e = new Exception();
//        String[] result_code_en = e.getResult_code_en();
//        String[] result_message = e.getResult_message_ch();
//        for (int i = 0; i < result_code_en.length; i++) {
////            if (resultCode.getRESULTCODE().contains("#")){
////                String[] resultCodes = resultCode.getRESULTCODE().split("#");
////                resultCode.setRESULTCODE(resultCodes[0]);
////            }
//            if (result_code_en[i].equals(resultCode.getRESULTCODE().equals("") ? resultCode.getRESULTMESSAGE() : resultCode.getRESULTCODE())){
//                message = result_message[i];
//                break;
//            }
//        }
//        if (FreeApi_StringUtils.isEmpty(message)){
//
//        }
        if (!FreeApi_StringUtils.isEmpty(resultCode.getRESULTMESSAGE())){
            message = resultCode.getRESULTMESSAGE();
            if (resultCode.getRESULTMESSAGE().contains("MES TimeOut")){
                message = "MES超时，请重新请求!";
            }
        }else {
            message = FreeApi_StringUtils.isEmpty(resultCode.getRESULTCODE()) ? "" : resultCode.getRESULTCODE();
        }

        return message.equals("") ? "服务器返回失败!" : message;
    }
}
